package com.css.project.oa.cash.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.css.common.utils.DateUtils;
import com.css.project.oa.cash.domain.ProCash;
import com.css.project.process.modeler.domain.HistoricActivity;
import com.css.project.process.modeler.service.IProcessService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.css.framework.aspectj.lang.annotation.Log;
import com.css.framework.aspectj.lang.enums.BusinessType;
import com.css.project.oa.cash.domain.ProCashAudit;
import com.css.project.oa.cash.service.IProCashAuditService;
import com.css.framework.web.controller.BaseController;
import com.css.framework.web.domain.AjaxResult;
import com.css.common.utils.poi.ExcelUtil;
import com.css.framework.web.page.TableDataInfo;

/**
 * 现金报销单审核Controller
 * 
 * @author css
 * @date 2020-05-15
 */
@RestController
@RequestMapping("/product/cashAudit")
public class ProCashAuditController extends BaseController
{
    @Autowired
    private IProCashAuditService proCashAuditService;

    @Autowired
    private IProcessService processService;

    /**
     * 查询现金报销单审核列表
     */
    @PreAuthorize("@ss.hasPermi('product:cashAudit:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProCash proCashAudit)
    {
        startPage();
        List<ProCash> list = proCashAuditService.selectProCashAuditList(proCashAudit);
        return getDataTable(list);
    }

    /**
     * 导出现金报销单审核列表
     */
    @PreAuthorize("@ss.hasPermi('product:cashAudit:export')")
    @Log(title = "现金报销单审核", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProCash proCashAudit)
    {
        List<ProCash> list = proCashAuditService.selectProCashAuditList(proCashAudit);
        for(ProCash model : list){
            model.setCashTimeStr(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,model.getCashTimeStart())+"至"+DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,model.getCashTimeEnd()));
        }
        ExcelUtil<ProCash> util = new ExcelUtil<ProCash>(ProCash.class);
        return util.exportExcel(list, "审核列表","auditExport");
    }


    /**
     * 修改现金报销单审核

    @PreAuthorize("@ss.hasPermi('product:cashAudit:edit')")
    @Log(title = "现金报销单审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProCash proCashAudit)
    {
        return toAjax(proCashAuditService.updateProCashAudit(proCashAudit));
    }*/

    /**
     * 现金报销单审核
     */
    @PreAuthorize("@ss.hasPermi('product:cashAudit:audit')")
    @Log(title = "现金报销单审核", businessType = BusinessType.UPDATE)
	@PutMapping("/audit")
    public AjaxResult audit(@RequestBody ProCashAudit proCashAudit)
    {
        return toAjax(proCashAuditService.proCashAudit(proCashAudit));
    }

    /**
     * 查询现金报销单已办列表
     */
    @PreAuthorize("@ss.hasPermi('product:cashAuditDone:list')")
    @GetMapping("/doneList")
    public TableDataInfo doneList(ProCash proCashAudit)
    {
        startPage();
        List<ProCash> list = proCashAuditService.selectProCashAuditDoneList(proCashAudit);
        return getDataTable(list);
    }

    /**
     * el时间轴根据流程查询，各任务节点
     */
    @GetMapping(value = "/selectTaskNode")//processInstanceId
    public AjaxResult selectTaskNode(String instanceId, HistoricActivity historicActivity, String auditOptionsKey)
    {
        List<HistoricActivity> list = processService.selectHistoryList(instanceId,historicActivity,auditOptionsKey) ;
        List<Map<String,String>> listMap = new ArrayList<>() ;
        for(HistoricActivity model :list){
            Map<String,String> map = new HashMap<>() ;
            String content = model.getActivityName() ;
            if(com.css.common.utils.StringUtils.isNotBlank(model.getAssigneeName())){
                content += "（"+model.getAssigneeName()+"）" ;
                if(model.getAuditOptions() != null){
                    content += "&nbsp;&nbsp;&nbsp;&nbsp;审核结果："+((Boolean)model.getAuditOptions()?"通过":"不通过");
                }
                if (com.css.common.utils.StringUtils.isNotBlank(model.getComment())) {
                    content += ","+model.getComment();
                }
                if(model.getEndTime() != null){
                    map.put("timestamp", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,model.getEndTime())) ;
                }else{//为空，一般就是还未办结
                    map.put("color","#0bbd87") ;
                }

            }else{//为空 说明代办
                map.put("color","#0bbd87") ;
            }
            map.put("content",content);
            listMap.add(map) ;
        }
        return AjaxResult.success(listMap);
    }

    /**
     * 导出现金报销单审核列表
     */
    @PreAuthorize("@ss.hasPermi('product:cashAuditDone:export')")
    @Log(title = "现金报销单已办导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exportDone")
    public AjaxResult exportDone(ProCash proCashAudit)
    {
        List<ProCash> list = proCashAuditService.selectProCashAuditDoneList(proCashAudit);
        for(ProCash model : list){
            model.setCashTimeStr(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,model.getCashTimeStart())+"至"+DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,model.getCashTimeEnd()));
        }
        ExcelUtil<ProCash> util = new ExcelUtil<ProCash>(ProCash.class);
        return util.exportExcel(list, "cashAudit","doneExport");
    }
}
